//  
//  Copyright (C) 2009 
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 

using System;
using Hyena;

using Banshee;
using Banshee.Base ;
using Banshee.Sources;
using Banshee.ServiceStack;
using Banshee.Library;

namespace Banshee.Plugins.LibraryWatcher
{	
	public class LibraryWatcherService : IExtensionService, IDisposable
	{
		private bool watcher_started = false;
		private static SourceWatcher watcher = new SourceWatcher ();
		
        string IService.ServiceName
        {
            get { return "Lybrary Watcher Plugin"; }
        }
		
        void IExtensionService.Initialize ()
        {
            Log.Debug ("Init library watcher plugin...");
            try {
                 if (!ServiceStarted ()) {
                    ServiceManager.ServiceStarted += OnServiceStarted;
                 } else {
                    this.StartWatch ();
                 }
             } catch (Exception ex) {
                Log.Error ("Error initializing library watcher: " + ex.Message);
            }
        }
		
		public void Dispose ()
        {
            ServiceManager.SourceManager.SourceAdded -= OnSourceAdded;
            if (watcher != null) {
                watcher.StopWatch ();
            }
            watcher_started = false;
        }

		private bool ServiceStarted ()
        {
            if (!ServiceManager.IsInitialized 
            || ServiceManager.Get<LibraryImportManager> () == null 
            || ServiceManager.SourceManager.MusicLibrary == null 
            || ServiceManager.SourceManager.VideoLibrary == null) {
                return false;
            }			
            return true;
        }

        private void OnSourceAdded (SourceEventArgs evt)
        {
            if (evt.Source is MusicLibrarySource) {
               watcher.MusicLibrary = (MusicLibrarySource) evt.Source;
            }
            if (evt.Source is VideoLibrarySource) {
                watcher.VideoLibrary = (VideoLibrarySource) evt.Source;
            }
			
            if (!watcher_started && watcher.MusicLibrary != null && watcher.VideoLibrary != null) {
                this.StartWatch ();
            }
        }
		
        private void StartWatch ()
        {	
            watcher.StartWatch ();
            watcher_started = true;
        }
		
        private void OnServiceStarted (ServiceStartedArgs args)
        {
            ServiceManager.SourceManager.SourceAdded += OnSourceAdded;
        }
    }
}
